package com.hcss.sys.client.controller;

import com.hcss.common.vo.ResultMsg;
import com.hcss.common.vo.Results;
import com.hcss.sys.client.constant.HcChrgClientConstants;
import com.hcss.sys.client.model.vo.HcChrgClientVO;
import com.hcss.sys.client.service.HcChrgClientService;
import com.hcss.util.AjaxResult;
import com.hcss.util.EmptyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @描述: 客户注册 功能模块 所有页面anon
 * @作者: fangtao
 * @创建日期: 2020-09-18 14:29:21
 */
@Slf4j
@RestController
@RequestMapping("clientRegistration")
public class ClientRegistrationController {
    @Autowired
    private HcChrgClientService hcChrgClientService;


    /**
     * 客户注册页面
     * @return
     */
    @RequestMapping("/registrationPage")
    public ModelAndView registrationPage() {
        ModelAndView page = new ModelAndView("/sys/client/registration/registration-listPage");
        return page;
    }

    /**
     * 校验客户数据
     */
    @PostMapping("/checkClientData")
    public AjaxResult checkClientData( HcChrgClientVO model) {
        try {
            //校验客户数据
            hcChrgClientService.checkClientData(model);
            return new AjaxResult();
        } catch (Exception e) {
            log.error("客户校验出错，参数：[{}] ", model.toString(), e);
            return AjaxResult.getErrorResult(e.getMessage());
        }
    }

    /**
     * 客户注册（新增并提交）
     * @param model
     * @return
     */
    @PostMapping("/addRegistration")
    public AjaxResult registration( HcChrgClientVO model) {
        //校验客户重复性
        if (!hcChrgClientService.checkClient(model)) {
            return AjaxResult.getErrorResult("客户已存在，请误重复提交！");
        }
        try {
            //新增
            model.setClientStatus( HcChrgClientConstants.CLIENTSTATUS.CLIENTSTATUS_99);//市场待审核状态
            model.setClientNcstatus( HcChrgClientConstants.CLIENTNCSTATUS.CLIENTNCSTATUS_99);//内场待审核状态
            model.setOpId(model.getUserid());
            model.setOpDate(new Date());
            hcChrgClientService.addClientVO(model);
            return new AjaxResult();
        } catch (Exception e) {
            log.error("客户注册出错，参数：[{}] ", model.toString(), e);
            return AjaxResult.getErrorResult(e.getMessage());
        }
    }

    /**
     * 附件上传（多个）
     *
     * @param file_data
     * @return
     */
    @RequestMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam MultipartFile[] file_data, String type) {
        if (ArrayUtils.isEmpty(file_data)) {
            return AjaxResult.getErrorResult("上传文件不能为空");
        }
        if (StringUtils.isBlank(type)) {
            return AjaxResult.getErrorResult("数据类型不能为空");
        }
        ResultMsg resultMsg = verifyFileType(file_data);
        if (Results.ERROR.equals(resultMsg.getState())) {
            return AjaxResult.getErrorResult(resultMsg.getMsg());
        }
        List<String> ids = new ArrayList<>();
        return AjaxResult.getSuccessResult(ids);
    }

    /**
     * 校验文件类型
     *
     * @param file_data
     * @return
     */
    private ResultMsg verifyFileType(MultipartFile[] file_data) {
        ResultMsg resultMsg = new ResultMsg(Results.SUCCESS);
        return resultMsg;
    }

    /**
     * 根据父级编码获取数据字典常量集
     * @param fatherCode
     * @return
     */
    @GetMapping("/getByFatherCode")
    public AjaxResult getByFatherCode(String fatherCode) {
        if (EmptyUtil.isHaveEmpty(fatherCode)) {
            return AjaxResult.getMissingParamResult();
        }
        return new AjaxResult();
    }
}
